<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class KcCount extends BaseController
{
    protected $title='库存汇总表';
    protected $table='stock_order_detail';
    private $sessionKey='pageKcCount';
//    public function index()
//    {
//        $param=$this->param;
//        if (request()->isAjax()) {
//
//            $where[]=['sod.status','=',2];
//            $where[]=['sod.type','in',['2','3']];
////            foreach (['bar_code','mdName','specs','brand'] as $i){
////                if (isset($param[$i])&&$param[$i]){
////                    $where[]=[$i,'like',"%".$param[$i].'%'];
////                }
////            }
//            if (isset($param['brand'])&&$param['brand']){
//                $where[]=['md.brand','like',"%".$param['brand'].'%'];
//            }
//            if (isset($param['mdName'])&&$param['mdName']){
//                $where[]=['md.name','like',"%".$param['mdName'].'%'];
//            }
//            if (isset($param['specs'])&&$param['specs']){
//                $where[]=['md.specs','like',"%".$param['specs'].'%'];
//            }
//            if (isset($param['buyCode'])&&$param['buyCode']){
//                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
//            }
//            if (isset($param['taxCode'])&&$param['taxCode']){
//                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
//            }
//            if (isset($param['cgrkd'])&&$param['cgrkd']){
//                $cgMxId=Db::name($this->table)
//                    ->where('type',3)
//                    ->where('status',2)
//                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
//                    ->column('saleContractDetailId');
//                $xsmxId=Db::name('sale_contract_detail')
//                    ->where('status',4)
//                    ->where('type',2)
//                    ->where('id','in',$cgMxId)
//                    ->column('saleContractDetailId');
//                $where[]=['saleContractDetailId','in',$xsmxId];
//            }
////            halt($where);
//            if (isset($param['cght'])&&$param['cght']){
//                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
//            }
//            if (isset($param['xsHt'])&&$param['xsHt']){
//                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
//            }
//
//            if (isset($param['pName'])&&$param['pName']){
//                $where[]=['p.name','like',"%".$param['pName'].'%'];
//            }
//            session($this->sessionKey,$where);
//            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
//            $list = Db::name($this->table)
//                ->alias(' sod')
//                ->leftJoin('material_dossier md','md.id=sod.mdId')
//                ->where($where)
//                ->field("sod.*,
//                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit")
//                ->paginate($rows)->each(function ($item,$index){
//                    $rukuCount=Db::name('stock_order_detail')
//                        ->where('status',2)
//                        ->where('mdId',$item['mdId'])
//                        ->where('type','in',[2,3])
//                        ->sum('num');
//                    $chukuCount=Db::name('stock_order_detail')
//                        ->where('status',2)
//                        ->where('mdId',$item['mdId'])
//                        ->where('type','in',[1,4])
//                        ->sum('num');
//                    $kcNum=$rukuCount-$chukuCount;
//                    $lockNum=Db::name('sale_contract_lock')
//                        ->where('status',1)
//                        ->where('mdId',$item['mdId'])
//                        ->sum('lockNum');
////
//                    $caigouCount=Db::name('sale_contract_detail')
//                        ->where('status',4)
//                        ->where('type','=',2)
//                        ->where('mdId',$item['mdId'])->sum('bjNum');//所有该物料的采购数量
//                      $canUse=$caigouCount-$lockNum;
//                    $kcMony=$this->getKcMoney($item['mdId']);
//                    $kcPrice=0;
//                    if ($rukuCount){
//                        $kcPrice=$kcMony/$rukuCount;
//                    }
//                    $item['kcNum']=$kcNum;
//                    $item['canUse']=$canUse;
//                    $item['kcPrice']=round($kcPrice,2);
//                    $item['kcMoney']=$kcMony;
//                    return $item;
//                });
//            return table_assign(0, '', $list);
//        } else {
//            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
//            View::assign('warehouseList',$warehouse);
//            return view();
//        }
//
//    }
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['sod.status','=',2];
            $where[]=['sod.type','in',['3']];
//            foreach (['bar_code','mdName','specs','brand'] as $i){
//                if (isset($param[$i])&&$param[$i]){
//                    $where[]=[$i,'like',"%".$param[$i].'%'];
//                }
//            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['md.brand','like',"%".$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like',"%".$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['md.specs','like',"%".$param['specs'].'%'];
            }
            if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
            }
            if (isset($param['taxCode'])&&$param['taxCode']){
                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
            }
            if (isset($param['cgrkd'])&&$param['cgrkd']){
                $cgMxId=Db::name($this->table)
                    ->where('type',3)
                    ->where('status',2)
                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
                    ->column('saleContractDetailId');
                $xsmxId=Db::name('sale_contract_detail')
                    ->where('status',4)
                    ->where('type',2)
                    ->where('id','in',$cgMxId)
                    ->column('saleContractDetailId');
                $where[]=['saleContractDetailId','in',$xsmxId];
            }
//            halt($where);
            if (isset($param['cght'])&&$param['cght']){
                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
            }
            if (isset($param['xsHt'])&&$param['xsHt']){
                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
            }

            if (isset($param['pName'])&&$param['pName']){
                $where[]=['p.name','like',"%".$param['pName'].'%'];
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias(' sod')
                ->leftJoin('stock_order so','so.id=sod.orderId' )
                ->leftJoin('material_dossier md','md.id=sod.mdId')
                ->where($where)
                ->field("sod.*,so.checkTime,
                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit")
                ->order('sod.id DESC')
                ->paginate($rows)->each(function ($item,$index){
                    $whear1[]=['id','=',$item['saleContractDetailId']];
                    $whear1[]=['status','=',4];
                    $sIds=Db::name('sale_contract_detail')
                        ->where($whear1)->column('saleContractDetailId');
                    $item['outNum']=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('saleContractDetailId','in',$sIds)
                        ->where('type','in',[1,4])->sum('num');
                    $cgDj=Db::name('sale_contract_detail')->where('id',$item['saleContractDetailId'])
                        ->value('bjPrice');
                    $item['hasNum']=$item['num']-$item['outNum'];
                    $item['kcMoney']=$cgDj*$item['hasNum'];
                    $dj='';
                    $je='';
                    if ($item['type']==3){
                        $dj=Db::name('sale_contract_detail')
                            ->where('id',$item['saleContractDetailId'])
                            ->value('bjPrice');
                        $je=round($dj*$item['hasNum'],2);
                    }
                    $item['kcPrice']=$dj;
                    $item['kcMoney']=$je;
                    $item['xsht']=Db::name('sale_contract_detail')
                        ->where('id',$item['saleContractDetailId'])->value('saleContractOrderNum');
                    $inTime=strtotime($item['checkTime']);

                    $item['kcDayLong']=ceil((time()-$inTime)/86400);
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
            View::assign('warehouseList',$warehouse);
            return view();
        }

    }


    public function getKcMoney($mdId)
    {
        $list=Db::name('stock_order_detail')
            ->alias('sod')
            ->leftJoin('sale_contract_detail scd','scd.id=sod.saleContractDetailId')
            ->where('sod.type','in',[2,3])
            ->where('sod.status','=',2)
            ->where('sod.mdId',$mdId)
            ->field('sod.num,scd.bjPrice')
            ->select();
        $money=0;
        foreach ($list as $v){
            $price=$v['num']*$v['bjPrice'];
            $money+=round($price,2);
        }
return $money;
    }
}